//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.Web;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Users;
using DotNetNuke.Services.Localization;
using DotNetNuke.Services.Messaging;
namespace DotNetNuke.UI.Skins.Controls
{
	partial class User : UI.Skins.SkinObjectBase
	{
		const string MyFileName = "User.ascx";
		private string _text;
		private string _cssClass;
		private string _url;
		bool _showUnreadMessages = true;
		public string Text {
			get { return _text; }
			set { _text = value; }
		}
		public string CssClass {
			get { return _cssClass; }
			set { _cssClass = value; }
		}
		public string URL {
			get { return _url; }
			set { _url = value; }
		}
		public bool ShowUnreadMessages
		{
			get { return _showUnreadMessages; }
			set { _showUnreadMessages = value; }
		}
		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			cmdRegister.Click += new EventHandler(cmdRegister_Click);

			try {
				if (!String.IsNullOrEmpty(CssClass)) {
					cmdRegister.CssClass = CssClass;
				}
				if (Request.IsAuthenticated == false) {
					if (PortalSettings.UserRegistration != (int)DotNetNuke.Common.Globals.PortalRegistrationType.NoRegistration) {
						if (!String.IsNullOrEmpty(Text)) {
							if (Text.IndexOf("src=") != -1) {
								Text = Text.Replace( "src=\"", "src=\"" + PortalSettings.ActiveTab.SkinPath);
							}
							cmdRegister.Text = Text;
						} else {
							cmdRegister.Text = Localization.GetString("Register", Localization.GetResourceFile(this, MyFileName));
						}
						if (PortalSettings.Users < PortalSettings.UserQuota || PortalSettings.UserQuota == 0)
						{
							cmdRegister.Visible = true;
						} else {
							cmdRegister.Visible = false;
						}
					} else {
						cmdRegister.Visible = false;
					}
				} else {
					UserInfo objUserInfo = UserController.GetCurrentUserInfo();
					if (objUserInfo.UserID != -1) {
						MessagingController messagingController = new MessagingController();
						int messageCount = messagingController.GetNewMessageCount(PortalSettings.PortalId, objUserInfo.UserID);
						cmdRegister.Text = objUserInfo.DisplayName;
						if ((ShowUnreadMessages && messageCount > 0))
						{
							cmdRegister.Text = cmdRegister.Text + string.Format(Localization.GetString("NewMessages", Localization.GetResourceFile(this, MyFileName)), messageCount);
						}
						if (ShowUnreadMessages && messageCount > 0)
                        {
                            cmdRegister.ToolTip = String.Format(Localization.GetString("ToolTipNewMessages", Localization.GetResourceFile(this, MyFileName)), messageCount);
                        }
                        else
                        {
                            cmdRegister.ToolTip = Localization.GetString("ToolTip", Localization.GetResourceFile(this, MyFileName));
                        }
					}
				}
			} catch (Exception exc) {
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		private void cmdRegister_Click(object sender, System.EventArgs e)
		{
			try {
				if (Request.IsAuthenticated == false) {
					if (PortalSettings.UserRegistration != (int)DotNetNuke.Common.Globals.PortalRegistrationType.NoRegistration) {
						if (!String.IsNullOrEmpty(URL)) {
							Response.Redirect(URL, true);
						} else {
							Response.Redirect(Common.Globals.RegisterURL(HttpUtility.UrlEncode(Common.Globals.NavigateURL()), Null.NullString), true);
						}
					}
				} else {
					UserInfo objUserInfo = UserController.GetCurrentUserInfo();
					if (objUserInfo.UserID != -1) {
						Response.Redirect(Common.Globals.UserProfileURL(objUserInfo.UserID));
					}
				}
			} catch (Exception exc) {
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
	}
}
